package com.woniu.service.impl;

import com.woniu.entity.Product;
import com.woniu.mapper.ProductMapper;
import com.woniu.service.ProductService;
import com.woniu.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ProductServiceImpl implements ProductService {
    SqlSession sqlSession = MyBatisUtil.getSqlSession();
    ProductMapper productMapper = sqlSession.getMapper(ProductMapper.class);
    @Override
    public List<Product> selectAllProduct() {
         List<Product> products = productMapper.selectProduct();
        return products;
    }

    @Override
    public Product selectProductById(int id) {
         Product product = productMapper.selectProductById(id);
        return product;
    }

    @Override
    public void UpdateProductStock(Integer pid, int stock) {
        productMapper.updateProductStock(pid, stock);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Product> selectProductByUid(int merchant_id) {
         List<Product> products = productMapper.selectProductByUid(merchant_id);
        return products;
    }

    @Override
    public void insertProduct(Product product) {
        productMapper.insertProduct(product);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void updateProductPrice(Integer pid, double price) {
        productMapper.updateProductPrice(pid, price);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteProductById(int pid) {
        productMapper.deleteProduct(pid);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteProductByMerchantId(int merchant_id) {
        productMapper.deleteProductByMerchantId(merchant_id);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public List<Product> selectProductByName(String pname) {
        List<Product> products = productMapper.selectProductByName(pname);
        return products;
    }

    @Override
    public Product selectProductMidName(Integer merchant_id, String pname) {
        Product product = productMapper.selectProductMidName(merchant_id, pname);
        return product;
    }


}
